<?php

namespace App\V3\V3_2_7\Controllers;

use App\Http\Controllers\Controller;
use App\V3\V3_2_7\Models\AlbumImgTitleModel;
use Illuminate\Http\Request;
use App\V3\V3_2_7\Models\SystemConfigModel;
use App\Helper\DBRSAUtil;
use App\V3\V3_2_7\Services\ContentTagsService;
use App\V3\V3_2_7\Services\TextImageService;
use App\V3\V3_2_7\Models\TextImageTemplatesModel;
use App\V3\V3_2_7\Services\PictureEditingService;
use Illuminate\Support\Facades\Validator;
use App\V3\V3_2_7\Models\ContentTagsModel;
use App\V3\V3_2_7\Services\ImageLabelService;
use App\V3\V3_2_7\Services\DispatchListService;
use App\V3\V3_2_7\Models\DeliverGoodsModel;
use App\V3\V3_2_7\Services\DeliverySlipService;
use App\V3\V3_2_7\Models\DeliveryGoodsModel;
use App\V3\V3_2_7\Models\QuotationGoodsModel;
use App\V3\V3_2_7\Services\ReceiptService;
use App\V3\V3_2_7\Services\MedicalDeliveryOrderService;
use App\V3\V3_2_7\Models\MedicalProductsModel;
use App\V3\V3_2_7\Services\MedicalDeliverOrderService;
use App\V3\V3_2_7\Models\MedicalDeliverProductsModel;
use App\V3\V3_2_7\Services\SealManageService;
use App\V3\V3_2_7\Models\QuotationTagModel;
use App\V3\V3_2_7\Models\QuotationNoModel;
use App\V3\V3_2_7\Services\HcMaterielTypeService;
use App\V3\V3_2_7\Models\GoodsAlbumModel;
use App\V3\V3_2_7\Models\AdminAddendumModel;
use OSS\OssClient;

class ContentTagsController extends Controller
{
    protected $ossClient;

    public function __construct(Request $request)
    {

        $this->ossClient = new OssClient(
            config('app.aliyun_oss_key'),
            config('app.aliyun_oss_secret'),
            config('app.aliyun_oss_endpoint')
        );
    }

    /**
     * 添加内容标签
     * @param pa_id        文字画册id
     * @param content_tags 标签名称
     * @param is_top       置顶
     * @param sort_number  排序数字
     */
    public function addContentTags(Request $request)
    {

        $res = self::loginCheck($request);

        if ($res['code'] == 2) {

            return response()->json($res);
        }

        $data = $request->all();

        if (empty($request->input('pa_id'))) {

            return response()->json(['code' => 1, 'msg' => '文字画册id，不能为空', 'data' => '']);
        }

        if (isset($data['sort_number']) && !is_numeric($data['sort_number'])) {

            return response()->json(['code' => 1, 'msg' => '排序字段，必须为数字', 'data' => '']);
        }

        if (!empty($data['is_top'])) {
            if ($data['is_top'] == 1) {
                return response()->json(['code' => 1, 'msg' => '已设置首页封面,为置顶显示！', 'data' => '']);
                /*  $condition = [];
                 $condition['is_top']       = 1;
                 $condition['pa_id']        = $request->input('pa_id');
                 $condition['user_id']      = $res['data']['userId'];
                 $condition['main_id']      = $res['data']['mainUserId'];

                 $info = ContentTagsService::getContentTagsInfo($condition);

                 if(!empty($info)){

                     /* ContentTagsService::updContentTags($condition, ['is_top'=>0]);

                 } */
            }
        }

        $data['create_time'] = time();
        $data['user_id'] = $res['data']['userId'];
        $data['main_id'] = $res['data']['mainUserId'];
        $data['pa_id'] = $request->input('pa_id');
        $data['is_top'] = !empty($data['is_top']) ? $data['is_top'] : 0;
        $data['sort_number'] = empty($data['sort_number']) ? '' : $data['sort_number'];
        $data['content_tags'] = empty($data['content_tags']) ? '' : $data['content_tags'];

        $id = ContentTagsService::addContentTags($data);

        if (!empty($id)) {

            return response()->json(['code' => 0, 'msg' => '保存成功', 'data' => ['ct_id' => $id]]);

        } else {
            return response()->json(['code' => 1, 'msg' => '保存失败', 'data' => '']);
        }

    }

    /**
     * 查询内容标签关联内容模板列表
     * @param curr_page 当前页
     * @param page_size 显示个数
     * @param pa_id     文字画册id
     * @param sort_number 排序数字
     */
    public function getTagsRelationContentList(Request $request)
    {

        $res = self::loginCheck($request);

        if ($res['code'] == 2) {

            return response()->json($res);
        }
        $currentPage = !empty($request->input('curr_page')) ? $request->input('curr_page') : 1;
        $pageSize = !empty($request->input('page_size')) ? $request->input('page_size') : 20;

        /* $condition['user_id'] = $res['data']['userId']; */
        $condition['pa_id'] = $request->input('pa_id');
        $sort_number = !empty($request->input('sort_number')) ? $request->input('sort_number') : '';
        $list = ContentTagsService::getContentTagsList($condition, $sort_number, $currentPage, $pageSize);

        if (!empty($list['data'])) {

            foreach ($list['data'] as $k => $v) {

                $tcondition['temp_tags_id'] = $v['id'];
                $tcondition['pa_id'] = $v['pa_id'];

                $tList = TextImageTemplatesModel::getContentTempLateList03($tcondition);

                if (!empty($tList)) {

                    foreach ($tList as $tk => $tv) {

                        $tList[$tk]['create_time'] = date('Y-m-d H:i:s', $tv['create_time']);
                        $tList[$tk]['update_time'] = date('Y-m-d H:i:s', $tv['update_time']);

                        if (!empty($tv['temp_img_video_01'])) {

                            $imgArr01 = explode(',', $tv['temp_img_video_01']);

                            foreach ($imgArr01 as $ik => $iv) {

                                $imgArr01[$ik] = self::addWatermark($this->ossClient, $iv, $res['data']['mainUserId'], $flag = 2);
                            }

                            $tList[$tk]['temp_img_video_01'] = $imgArr01;
                        }

                        if (!empty($tv['temp_img_video_02'])) {

                            $imgArr02 = explode(',', $tv['temp_img_video_02']);

                            foreach ($imgArr02 as $ik => $iv) {

                                $imgArr02[$ik] = self::addWatermark($this->ossClient, $iv, $res['data']['mainUserId'], $flag = 2);
                            }

                            $tList[$tk]['temp_img_video_02'] = $imgArr02;
                        }

                    }
                    $list['data'][$k]['text_content_list'] = $tList;

                } else {

                    $list['data'][$k]['text_content_list'] = [];
                }
            }
        }

        return response()->json(['code' => 0, 'msg' => '查询成功', 'data' => $list]);
    }

    /**
     * 修改内容标签
     * @param ct_id        内容标签id
     * @param content_tags 标签名称
     * @param is_top       置顶
     * @param sort_number  排序数字
     */
    public function updContentTags(Request $request)
    {

        if (empty($ctId = $request->input('ct_id'))) {

            return response()->json(['code' => 1, 'msg' => '内容标签id,不能为空', 'data' => '']);
        }
        if (empty($paId = $request->input('pa_id'))) {

            return response()->json(['code' => 1, 'msg' => '文字画册id,不能为空', 'data' => '']);
        }
        $res = self::loginCheck($request);

        if ($res['code'] == 2) {

            return response()->json($res);
        }

        $condition['id'] = $ctId;
        //$condition['user_id'] = $res['data']['userId'];

        $updData = $request->all();
        $updData['update_time'] = time();

        if (!empty($updData['is_top'])) {
            if ($updData['is_top'] == 1) {
                return response()->json(['code' => 1, 'msg' => '已设置首页封面,为置顶显示！', 'data' => '']);
            }
        }

        /* $updCondition['is_top']  = 1;
        $updCondition['pa_id']   = $paId;
        $updCondition['user_id'] = $res['data']['userId'];

        $info = ContentTagsService::getContentTagsInfo($updCondition);

        if(!empty($info)){

            if(isset($updData['is_top'])){

                if($updData['is_top'] == 1){

                    ContentTagsService::updContentTags($updCondition,['is_top'=> 0]);
                }
            }
        } */

        unset($updData['ct_id']);

        $bool = ContentTagsService::updContentTags($condition, $updData);

        if ($bool) {

            return response()->json(['code' => 0, 'msg' => '修改成功', 'data' => '']);
        }

        return response()->json(['code' => 1, 'msg' => '修改失败', 'data' => '']);

    }

    /**
     * 删除内容标签
     * @param ct_id 内容标签id
     */
    public function delContentTags(Request $request)
    {

        if (empty($ctId = $request->input('ct_id'))) {

            return response()->json(['code' => 1, 'msg' => '内容标签id,不能为空', 'data' => '']);
        }

        $res = self::loginCheck($request);

        if ($res['code'] == 2) {

            return response()->json($res);
        }

        $condition['id'] = $ctId;
        //$condition['user_id'] = $res['data']['userId'];
        $info = ContentTagsService::getContentTagsInfo($condition);

        if ($info['is_top'] == 1) {

            return response()->json(['code' => 1, 'msg' => '首页封面标签，不能删除', 'data' => '']);
        }

        $bool = ContentTagsService::delContentTags($condition);

        if ($bool) {

            return response()->json(['code' => 0, 'msg' => '删除成功', 'data' => '']);
        }

        return response()->json(['code' => 1, 'msg' => '删除失败', 'data' => '']);

    }

    /**
     * 查询内容标签信息
     * @param ct_id 内容标签id
     */
    public function getContentTagsInfo(Request $request)
    {
        //参数验证
        $validator = Validator::make($data = $request->all(), [
            'ct_id' => 'required'
        ],
            [
                'ct_id.required' => '缺少参数，内容标签id'
            ]);
        //参数错误响应
        if ($info = $validator->errors()->first()) {

            return response()->json(['code' => 1, 'msg' => $info, 'data' => '']);
        }

        $condition['id'] = $data['ct_id'];

        $info = ContentTagsService::getContentTagsInfo($condition);

        if (!empty($info)) {
            $tcondition['act.pa_id'] = $info['pa_id'];
            $tcondition['act.temp_tags_id'] = $data['ct_id'];

            $list = TextImageTemplatesModel::getContentTempLateList02($tcondition);

            if (!empty($list)) {
                foreach ($list as $k => $v) {
                    $list[$k]['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
                    $list[$k]['update_time'] = date('Y-m-d H:i:s', $v['update_time']);

                    if (!empty($v['temp_img_video_01'])) {

                        $imgArr01 = explode(',', $v['temp_img_video_01']);

                        foreach ($imgArr01 as $ik => $iv) {

                            $imgArr01[$ik] = self::addWatermark($this->ossClient, $iv, $info['main_id'], $flag = 2);
                        }

                        $list[$k]['temp_img_video_01'] = $imgArr01;
                    }

                    if (!empty($v['temp_img_video_02'])) {

                        $imgArr02 = explode(',', $v['temp_img_video_02']);

                        foreach ($imgArr02 as $ik => $iv) {

                            $imgArr02[$ik] = self::addWatermark($this->ossClient, $iv, $info['main_id'], $flag = 2);
                        }

                        $list[$k]['temp_img_video_02'] = $imgArr02;
                    }
                }
            }
            $info['content_temp_list'] = $list;

            return response()->json(['code' => 0, 'msg' => '查询成功', 'data' => $info]);
        } else {

            return response()->json(['code' => 1, 'msg' => '查询失败', 'data' => '']);
        }

    }

    /**
     * 查询图片画册标签信息
     * @param label_id 内容标签id
     */
    public function getImageLabelInfo(Request $request)
    {
        //参数验证
        $validator = Validator::make($data = $request->all(), [
            'label_id' => 'required'
        ],
            [
                'label_id.required' => '缺少参数，内容标签id'
            ]);
        //参数错误响应
        if ($info = $validator->errors()->first()) {

            return response()->json(['code' => 1, 'msg' => $info, 'data' => '']);
        }

        $condition['id'] = $data['label_id'];

        $info = ImageLabelService::getImageLabelInfo($condition);

        if (!empty($info)) {

            if (!empty($info['Album_imgs'])) {

                //$info['Album_imgs'] = explode(',',$info['Album_imgs']);
                $imgArr = explode(',', $info['Album_imgs']);

                foreach ($imgArr as $iak => $iav) {

                    $imgArr[$iak] = self::addWatermark($this->ossClient, $iav, $info['main_id']);
                }

                $info['Album_imgs'] = $imgArr;

            }

            $info['create_time'] = date('Y-m-d H:i:s', $info['create_time']);
            $info['update_time'] = date('Y-m-d H:i:s', $info['update_time']);

            return response()->json(['code' => 0, 'msg' => '查询成功', 'data' => $info]);
        } else {

            return response()->json(['code' => 1, 'msg' => '查询失败', 'data' => '']);
        }

    }

    /**
     * 查询图片画册信息
     * @param pa_id 画册的id
     */
    public function getPictureAlbumInfo(Request $request)
    {

        $validator = Validator::make(
            $data = $request->all(), [
            'pa_id' => 'required'
        ], [
            'pa_id.required' => '画册的id,不能为空!'
        ]);
        //参数错误响应
        if ($info = $validator->errors()->first()) {

            return response()->json(['code' => 1, 'msg' => $info, 'data' => '']);
        }

        $condition['id'] = $data['pa_id'];

        $info = PictureEditingService::getPictureAlbumInfo($condition);

        if (empty($info)) {

            return response()->json(['code' => 1, 'msg' => '查询的图片画册，没有找到！', 'data' => '']);
        }

        if (!empty($info['album_cover_img'])) {

            $info['album_cover_img'] = self::addWatermark($this->ossClient, $info['album_cover_img'], $info['main_id']);
        }
        $condition['pa_id'] = $data['pa_id'];

        unset($condition['id']);
        $iList = ImageLabelService::getImageLabelLists($condition);

        $info['label_data'] = [];

        if (!empty($iList)) {

            $info['label_data'] = $iList;

            foreach ($info['label_data'] as $ik => $iv) {

                $info['label_data'][$ik]['id'] = $iv['id'];
                $info['label_data'][$ik]['pa_id'] = $iv['pa_id'];
                $info['label_data'][$ik]['label_name'] = $iv['label_name'];

                if (!empty($iv['Album_imgs'])) {

                    $imgArr = explode(',', $iv['Album_imgs']);

                    foreach ($imgArr as $iak => $iav) {

                        $imgArr[$iak] = self::addWatermark($this->ossClient, $iav, $info['main_id']);
                    }
                    $info['label_data'][$ik]['Album_imgs'] = $imgArr;
                } else {
                    $info['label_data'][$ik]['Album_imgs'] = [];
                }
            }
        }
        $info['create_time'] = date('Y-m-d H:i:s', $info['create_time']);
        $info['update_time'] = date('Y-m-d H:i:s', $info['update_time']);
        $info['page_img_list'] = AlbumImgTitleModel::query()
            ->select(
                "album_id",
                "img_title",
                "album_img",
                "flag"
            )
            ->where([
                'flag' => 'img',
                'delete_flag' => 0,
                'album_id' => $info['id'],
                'main_id' => $info['main_id']
            ])->get()->toArray() ?? [];
        return response()->json(['code' => 0, 'msg' => '查询成功', 'data' => $info]);

    }

    /**
     * 查询文字画册信息
     * @param pa_id 文字画册id
     */
    public function getTextImageInfo(Request $request)
    {
        //参数验证
        $validator = Validator::make($data = $request->all(), [
            'pa_id' => 'required'
        ],
            [
                'pa_id.required' => '缺少参数，文字画册id'
            ]);
        //参数错误响应
        if ($info = $validator->errors()->first()) {

            return response()->json(['code' => 1, 'msg' => $info, 'data' => '']);
        }

        $condition['id'] = $data['pa_id'];

        $info = TextImageService::getTextImageInfo($condition);

        if (!empty($info)) {
            if (!empty($info['album_cover_img'])) {

                $info['album_cover_img'] = self::addWatermark($this->ossClient, $info['album_cover_img'], $info['main_id']);
            }
            $tcondition['pa_id'] = $data['pa_id'];

            $list = ContentTagsModel::getContentTagsList02($tcondition);

            if (!empty($list)) {

                foreach ($list as $k => $v) {
                    unset($tcondition['user_id']);
                    $tcondition['temp_tags_id'] = $v['id'];
                    $tcondition['pa_id'] = $v['pa_id'];

                    $tList = TextImageTemplatesModel::getContentTempLateList03($tcondition);

                    if (!empty($tList)) {

                        foreach ($tList as $tk => $tv) {

                            $tList[$tk]['create_time'] = date('Y-m-d H:i:s', $tv['create_time']);
                            $tList[$tk]['update_time'] = date('Y-m-d H:i:s', $tv['update_time']);
                            $tList[$tk]['page_info'] = AlbumImgTitleModel::query()
                                ->select(
                                    "album_id",
                                    "img_title",
                                    "album_img",
                                    "flag"
                                )
                                ->where([
                                    'flag' => 'text',
                                    'delete_flag' => 0,
                                    'album_img' => $tv['id'],
                                    'main_id' => $info['main_id']
                                ])->first() ?? '';
                            if (!empty($tv['temp_img_video_01'])) {

                                $imgArr01 = explode(',', $tv['temp_img_video_01']);

                                foreach ($imgArr01 as $ik => $iv) {

                                    $imgArr01[$ik] = self::addWatermark($this->ossClient, $iv, $info['main_id']);
                                }

                                $tList[$tk]['temp_img_video_01'] = $imgArr01;
                            }

                            if (!empty($tv['temp_img_video_02'])) {

                                $imgArr02 = explode(',', $tv['temp_img_video_02']);

                                foreach ($imgArr02 as $ik => $iv) {

                                    $imgArr02[$ik] = self::addWatermark($this->ossClient, $iv, $info['main_id']);
                                }

                                $tList[$tk]['temp_img_video_02'] = $imgArr02;
                            }

                        }
                        $list[$k]['text_content_list'] = $tList;

                    } else {

                        $list[$k]['text_content_list'] = [];
                    }
                }
            }

            $info['content_tags_list'] = $list;
            return response()->json(['code' => 0, 'msg' => '查询成功', 'data' => $info]);
        } else {

            return response()->json(['code' => 1, 'msg' => '查询失败', 'data' => '']);
        }

    }

    /**
     * 发货单号 （打印、分享，不用传token）
     *
     * @param dispatch_id 发货单号id
     */
    public function dispatchListInfo(Request $request)
    {
        //参数验证
        $validator = Validator::make($data = $request->all(), [
            'dispatch_id' => 'required|integer',//发货单id
        ]);
        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return response()->json([
                'code' => 1,
                'msg' => $info,
                'data' => '',
            ]);
        }

        $condition['id'] = $data['dispatch_id'];

        $info = DispatchListService::getDispatchListInfo($condition);
        $list = $info;
        if (!empty($info)) {

            $list['goods_list'] = DeliverGoodsModel::where(['dispatch_id' => $info['id']])->get();

        }

        return response()->json([
            'code' => 0,
            'msg' => 'success',
            'data' => $list,
        ]);

    }

    /**
     * 送货单信息（打印、分享，不用传token）
     *
     * @param delivery_id 送货单号id
     */
    public function deliverySlipInfo(Request $request)
    {
        //参数验证
        $validator = Validator::make($data = $request->all(), [
            'delivery_id' => 'required|integer',//送货单id
        ]);
        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return response()->json([
                'code' => 1,
                'msg' => $info,
                'data' => '',
            ]);
        }

        $condition['id'] = $data['delivery_id'];

        $info = DeliverySlipService::getDeliverySlipInfo($condition);
        $list = $info;
        if (!empty($info)) {

            $list['goods_list'] = DeliveryGoodsModel::where(['delivery_id' => $info['id']])->get();
        }
        return response()->json(['code' => 0, 'msg' => 'success', 'data' => $list]);

    }

    /**
     * 报价单信息（打印、分享，不用传token）
     * @param quotation_id
     */
    public function quotationNoInfo(Request $request)
    {
        //参数验证
        $validator = Validator::make($data = $request->all(), [
            'quotation_id' => 'required|integer',//报价单id
        ]);
        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return response()->json([
                'code' => 1,
                'msg' => $info,
                'data' => '',
            ]);
        }

        $condition['id'] = $data['quotation_id'];

        $list = [];

        $info = QuotationNoModel::where($condition)->first()->toArray();
        if (!empty($info)) {

            $tag_list = QuotationTagModel::select("id", "tag_name")->where(['quotation_id' => $info['id']])->get()->toArray();

            if (!empty($tag_list)) {

                foreach ($tag_list as $k => $v) {
                    $list['tag_list'][$k]['id'] = $v["id"];
                    $list['tag_list'][$k]['name'] = $v["tag_name"];
                    $goodsList = QuotationGoodsModel::where(['tag_id' => $v['id']])->get()->toArray();
                    if (!empty($goodsList)) {
                        /*  foreach ($goodsList as $gk=>$gv){

                             $goodsList[$gk]['goods_img'] = self::addWatermark($this->ossClient,$gv['goods_img'],$info['main_id']);
                         } */
                    } else {
                        $goodsList = [];
                    }
                    $list['tag_list'][$k]['goods_list'] = $goodsList;
                }
            }

            $list = array_merge($info, $list);

            return response()->json(['code' => 0, 'msg' => 'success', 'data' => $list]);

        } else {

            return response()->json(['code' => 0, 'msg' => '此报价单，没有找到！', 'data' => ""]);
        }


    }

    /**
     * 收据信息（打印、分享，不用传token）
     * @param receipt_id
     */
    public function receiptInfo(Request $request)
    {
        //参数验证
        $validator = Validator::make($data = $request->all(), [
            'receipt_id' => 'required|integer',//收据id
        ]);
        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return response()->json([
                'code' => 1,
                'msg' => $info,
                'data' => '',
            ]);
        }

        $condition['id'] = $data['receipt_id'];


        $info = ReceiptService::getReceiptInfo($condition);

        return response()->json(['code' => 0, 'msg' => 'success', 'data' => $info]);

    }

    /**
     * 医疗器械专用送货单信息（打印、分享，不用传token）
     * @param medical_delivery_id
     */
    public function medicalDeliveryInfo(Request $request)
    {
        //参数验证
        $validator = Validator::make($data = $request->all(), [
            'medical_delivery_id' => 'required|integer',//医疗器械专用送货单id
        ]);
        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return response()->json([
                'code' => 1,
                'msg' => $info,
                'data' => '',
            ]);
        }

        $condition['id'] = $data['medical_delivery_id'];

        $info = MedicalDeliveryOrderService::getMedicalDeliveryOrderInfo($condition);
        $list = $info;
        if (!empty($info)) {

            $list['goods_list'] = MedicalProductsModel::where(['medical_delivery_id' => $info['id']])->get();

        }

        return response()->json(['code' => 0, 'msg' => 'success', 'data' => $list]);
    }

    /**
     * 医疗器械专用发货单信息（打印、分享，不用传token）
     * @param medical_deliver_id
     */
    public function medicalDeliverInfo(Request $request)
    {
        //参数验证
        $validator = Validator::make($data = $request->all(), [
            'medical_deliver_id' => 'required|integer',//医疗器械专用发货单id
        ]);
        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return response()->json([
                'code' => 1,
                'msg' => $info,
                'data' => '',
            ]);
        }

        $condition['id'] = $data['medical_deliver_id'];

        $info = MedicalDeliverOrderService::getMedicalDeliverOrderInfo($condition);
        $list = $info;
        if (!empty($info)) {

            $list['goods_list'] = MedicalDeliverProductsModel::where(['medical_deliver_id' => $info['id']])->get();

        }

        return response()->json(['code' => 0, 'msg' => 'success', 'data' => $list]);
    }


    /**
     * 印章信息（打印、分享，不用传token）
     * @param seal_id
     */
    public function sealInfo(Request $request)
    {
        //参数验证
        $validator = Validator::make($data = $request->all(), [
            'seal_id' => 'required|integer',//印章id
        ]);
        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return response()->json([
                'code' => 1,
                'msg' => $info,
                'data' => '',
            ]);
        }

        $condition['id'] = $data['seal_id'];
        $info = SealManageService::getSealManageInfo($condition);

        return response()->json(['code' => 0, 'msg' => 'success', 'data' => $info]);

    }


    /**
     * 商品画册列表
     * @param Request $request
     */
    public function productPicAlbumList(Request $request)
    {
        $aInfo = GoodsAlbumModel::select('id', 'p_a_name', 'main_id', 'album_cover_img', 'mobile', 'background_music', 'flag', 'album_logo')
            ->where(['id' => $request->album_id])->where(['is_del' => 0])->first();

        $adminInfo = AdminAddendumModel::select('is_vip', 'album_page_amount')->where(['main_id' => $aInfo['main_id']])->first()->toArray();

        if (!empty($adminInfo) && in_array($adminInfo['is_vip'], [1, 2])) {

            if ($adminInfo['album_page_amount'] != -1 && $request->current_page >= $adminInfo['album_page_amount']) {

                return response()->json([
                    'code' => 1,
                    'msg' => '您当前彩页超出限制' . $adminInfo['album_page_amount'] . '页，超出部分未生成彩页',
                    'data' => '',
                ]);
            }
        } else {
            return response()->json([
                'code' => 1,
                'msg' => '您还不是VIP会员，不能展示彩页！',
                'data' => '',
            ]);
        }

        $validator = Validator::make($data = $request->all(), [
            'album_id' => 'required|integer'//商品画册id
        ],
            [
                'album_id.required' => '商品画册id,必要参数',
                'album_id.integer' => '商品画册id,类型为正整数',
            ]
        );

        if ($info = $validator->errors()->first()) {
            return response()->json([
                'code' => 1,
                'msg' => $info,
                'data' => '',
            ]);
        }

        $keyword = empty($request->keyword) ? '' : $request->keyword;
        $condition = [];
        $nextClassIdArr = [];
        $condition1 = [];

        if (!empty($request->materiel_class_id)) {
            $nextClassIdArr[] = $request->materiel_class_id;
        }
        if (!empty($request->parent_class_id)) {

            $next_class_list = HcMaterielTypeService::nextClass($aInfo['main_id'], $request->parent_class_id);

            if (!empty($next_class_list)) {

                foreach ($next_class_list as $k => $v) {

                    $nextClassIdArr[$k] = $v['id'];
                }
            }
        }

        if (!empty($aInfo)) {
            if (!empty($aInfo['album_cover_img'])) {

                $aInfo['album_cover_img'] = self::addWatermark($this->ossClient, $aInfo['album_cover_img'], $aInfo['main_id']);
            }
            if (!empty($aInfo['album_logo'])) {

                $aInfo['album_logo'] = self::addWatermark($this->ossClient, $aInfo['album_logo'], $aInfo['main_id']);
            }
        } else {
            return response()->json(['code' => 1, 'msg' => '此商品画册，没有找到！', 'data' => ""]);
        };

        $condition['is_album'] = 1;

        $condition['album_id'] = $request->album_id;

        $list = HcMaterielTypeService::HcmaterielTypeSelect(
            $aInfo['main_id'],
            $keyword,
            $condition,
            $nextClassIdArr,
            $request->page_size,
            $request->current_page
        );
        $data = [];
        $data['pageSize'] = $request->page_size;
        $data['currentPage'] = $request->current_page;
        $list = HcMaterielTypeService::withSearch($list['data'], $aInfo['main_id'], $data, $condition, $condition1, $keyword);

        if (!empty($list['data'])) {
            foreach ($list['data'] as $k01 => $v01) {
                if (!empty($v01['next_class'])) {
                    foreach ($v01['next_class'] as $k02 => $v02) {
                        if (!empty($v02['next_hc_type'])) {
                            foreach ($v02['next_hc_type'] as $k03 => $v03) {
                                if (!empty($v03['materiel_img'])) {
                                    $list['data'][$k01]['next_class'][$k02]['next_hc_type'][$k03]['materiel_img'] = self::addWatermark($this->ossClient, $v03['materiel_img'], $aInfo['main_id']);
                                }
                            }
                        }
                    }
                }
            }
        }
        return response()->json([
            'code' => 0,
            'msg' => 'success',
            'data' => [
                'album_data' => $aInfo,
                'list' => $list
            ]
        ]);
    }

    /**
     * 添加水印
     * @param unknown $file
     */
    static public function addWatermark($client, $file, $mainId, $flag = 1)
    {

        $adminInfo = AdminAddendumModel::select('clear_watermark_or_not', 'account_storage', 'file_total_size')
            ->where(['main_id' => $mainId])->first();
        if ($flag == 1) {//如果占用空间大于设置空间，画册分享不显示
            if ($adminInfo['file_total_size'] > $adminInfo['account_storage']) return '';
        }

        if (empty($file)) return $file;

        if ($adminInfo['clear_watermark_or_not'] == 1) {

            return $file;
        } else {
            try {
                $delPath = strstr($file, 'uploads/');
                $imgInfo = $client->getObjectMeta(config('app.aliyun_oss_bucket'), $delPath);

                if (strpos($imgInfo['content-type'], 'image/') !== false) {

                    return $file = $file . config('app.aliyun_oss_img_watermark');
                }
                return $file;
            } catch (\Exception $e) {

            }
        }

    }

    static public function loginCheck($request)
    {

        $token = $request->header('token');

        if (empty($token)) {

            return ['code' => 2, 'msg' => 'token不能为空', 'data' => ''];
        }
        $array = explode(' ', $token);
        //私钥获取
        $private = SystemConfigModel::getConfigValue('private');
        $rsadata = DBRSAUtil::rsaDecrypt($array[1], $private);
        $rsadata = json_decode($rsadata, true);

        return ['code' => 0, 'msg' => 'success', 'data' => $rsadata];
    }
}
